package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.log.score.BaseScoreLog;
import com.qdlc.p2p.biz.log.score.identify.RealnameScoreLog;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.UserIdentifyDao;
import com.qdlc.p2p.dal.dao.VerifyLogDao;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.dto.VerifyLog;
import com.qdlc.p2p.dal.model.UserIdentifyModel;
import com.qdlc.p2p.dal.model.UserModel;

/**
 * 认证信息
 *
 * @author sj
 * @version 2.0
 * @since 2014年2月17日17:27:02
 */
@Service("userIdentifyService")
public class UserIdentifyServiceImpl implements UserIdentifyService {

	@Resource
	private UserIdentifyDao userIdentifyDao;

	@Resource
	private VerifyLogDao verifyLogDao;

	@Override
	public UserIdentify findByUserId(long userId) {
		return userIdentifyDao.findByUserId(userId);
	}

	@Override
	public void modifyRealnameStatus(long userId, int status, int preStatus) {
		userIdentifyDao.modifyRealnameStatus(userId, status, preStatus);
	}

	@Override
	public void modifyTppMobilePhoneStatus(long userId, int status, int preStatus) {
		UserIdentify identify = findByUserId(userId);
		// 状态是否变更，如有变更则修改
		if (identify.getTppMobilePhoneStatus() != status) {
			userIdentifyDao.modifyTppMobilePhoneStatus(userId, status, preStatus);
		}
	}

	@Override
	public void modifyTppAuthorizeStatus(long userId, boolean authorize, String authorizeType) throws Exception {
		UserIdentify identify = findByUserId(userId);
		// 状态是否变更，如有变更则修改
		if (identify.isAuthorize() != authorize) {
			identify.setAuthorize(authorize);
			identify.setAuthorizeType(authorizeType);
			userIdentifyDao.update(identify);
		}
	}

	@Override
	public UserIdentify getUserIdentifyByUserId(long userId) {
		return userIdentifyDao.findByUserId(userId);
	}

	@Override
	public void userAttestationEdit(long id, String realNameVerifyRemark, int realNameStatus, Operator operator) {
		// 添加审核日志
		VerifyLog verifyLog = new VerifyLog(operator, "realName", id, 2, realNameStatus, realNameVerifyRemark);
		verifyLogDao.save(verifyLog);
		UserIdentify identify = userIdentifyDao.find(id);
		if (realNameStatus == 1) {
			BaseScoreLog bLog = new RealnameScoreLog(identify.getUserId());
			bLog.doEvent();
		}
		userIdentifyDao.userAttestationEdit(id, identify.getUserId(), realNameVerifyRemark, realNameStatus, operator);
	}

	@Override
	public int countByRealName(int status) {
		return userIdentifyDao.countByRealName(status);
	}

	/**
	 * 通过ID查询记录
	 *
	 * @param id
	 * @return
	 */
	@Override
	public UserIdentify findById(long id) {
		return userIdentifyDao.find(id);
	}

	public PageDataList<UserIdentify> list(int pageNumber, int pageSize, UserIdentify model) {
		QueryParam param = QueryParam.getInstance().addPage(pageNumber, pageSize);
		param.addParam("vipStatus", model.getVipStatus());
		return userIdentifyDao.findPageList(param);
	}

	@Override
	public PageDataList<UserIdentifyModel> findUserIdentifylist(UserIdentifyModel model) {
		return userIdentifyDao.findUserIdentifylist(model);
	}

	@Override
	public PageDataList<UserIdentifyModel> findUserIdentifylist(int pageNumber, int pageSize, UserIdentify model,
			String searchName) {
		return userIdentifyDao.findUserIdentifylist(pageNumber, pageSize, model, searchName);
	}

	@Override
	public PageDataList<UserModel> userModelList(int pageNumber, int pageSize, UserIdentify model, String searchName) {
		return userIdentifyDao.userModelList(pageNumber, pageSize, model, searchName);
	}
}
